package com.yuanxiao.common.core.result;

import com.yuanxiao.common.core.exception.RRException;

import java.util.HashMap;

/**
 * 返回数据
 */
public class R extends HashMap<String, Object> {
	private static final long serialVersionUID = 1L;

	public R() {
		put("code", ResultCode.SUCCESS.getCode ());
		put("message", "success");
	}

    public static void throwMsg(boolean bool, String msg){
        if (!bool) {
            throw new RRException(msg);
        }
    }

    public static void throwR(R r){
        if (!R.isSuccess(r)) {
            throw new RRException(r.getMsg ());
        }
    }

	public static R error() {
		return error(ResultCode.FAIL.getCode (), "未知异常");
	}
	
	public static R error(String msg) {
		return error(ResultCode.FAIL.getCode (), msg);
	}
	
	public static R error(String code, String msg) {
		R r = new R();
		r.put("code", code);
		r.put("message", msg);
		return r;
	}

	public static R ok(String msg) {
		R r = new R();
		r.put("message", msg);
		return r;
	}

	public static R ok() {
		return new R();
	}

	public static R failed(IResultCode resultCode) {
		R r = new R();
		r.put("code", resultCode.getCode ());
		r.put("message", resultCode.getMsg ());
		return r;
	}

	public static R failed(String msg) {
		R r = new R();
		r.put("code", ResultCode.SYSTEM_EXECUTION_ERROR.getCode());
		r.put("message", msg);
		return r;
	}

	public static R failed(IResultCode resultCode, String msg) {
		R r = new R();
		r.put("code", resultCode.getCode ());
		r.put("message", msg);
		return r;
	}

    private static R returnWrapper(String code, String msg, Object... obj) {
        R r = new R();
        r.put("message", msg);
        if(obj != null && obj.length != 0) {
            if(obj.length == 1) {
                r.put("data", obj[0]);
            } else {
                r.put("data", obj);
            }
        } else {
            r.put("data", null);
        }
        r.put("code", code);
        return r;
    }

    public static R returnDataWrapper(Object... obj) {
        return returnSuccessWrapper(obj);
    }

    public static R returnBoolWrapper(boolean bool, String fails, Object... obj) {
        if (bool) {
            return returnSuccessWrapper(obj);
        }
        return returnFailsWrapper(fails, obj);
    }

    public static R returnSuccessWrapper(Object... obj) {
        return returnWrapper(ResultCode.SUCCESS.getCode (), "success", obj);
    }

    public static R returnFailsWrapper(String message, Object... obj) {
        return returnWrapper(ResultCode.FAIL.getCode (), message, obj);
    }

    public static boolean isSuccess(R r){
		return ResultCode.SUCCESS.getCode ().equals (r.get ("code").toString());
	}

	public static R judge(boolean status) {
		if (status) {
			return ok ();
		} else {
			return error ();
		}
	}

	public R put(String key, Object value) {
		super.put(key, value);
		return this;
	}

	public R setData(Object value) {
		super.put("data", value);
		return this;
	}

	public Object getData() {
		return this.get("data");
	}

	public String getCode(){
		return this.get("code").toString();
	}

	public String getMsg(){
		return this.get("message").toString();
	}

    public R setTransientData(Object value) {
        super.put("transientData", value);
        return this;
    }

}
